<script>
import HuilanUploader from "@/components/uploader/index.vue";
import AddSound from "@/views/aihuman/HumanAssets/soundType/AddSound.vue";
import {changeActionStatus, deleteAction, getActionPageList} from "@/api/aihuman/humanAssets/actionType";
import AddAction from "@/views/aihuman/HumanAssets/actionType/AddAction.vue";
import AddActionBatch from "@/views/aihuman/HumanAssets/actionType/AddActionBatch.vue";

export default {
  name: "ActionTypeList",
  components: {AddActionBatch, AddAction, AddSound, HuilanUploader},
  data() {
    //检验声音id
    var checkType = (rule, value, callback) => {
      // var inputType = /[^\w]/g
      var inputReg = /[a-zA-Z0-9-_]/g;
      if (!inputReg.test(value)) {
        callback(new Error('声音id由数字、英文、短划线、下划线组成'))
      } else {
        callback()
      }
    };
    return {
      groupId: '',
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      form: {},
      audioData: '',
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100],
      },
      tableData: [],
      tableOptions: {
        //新增按钮
        addBtn: false,
        header: false,
        viewBtn: false,
        height: 'auto',
        delBtn: false,
        editBtn: false,
        refreshBtn: false,
        columnBtn: false,
        searchShowBtn: false,
        stripe: false,
        selection: true,
        tip: false,
        // simplePage: false,
        searchMenuSpan: 8,
        border: true,
        index: true,
        indexWidth: 60,
        indexLabel: "序号",
        // menuWidth: 252,
        menu: false,
        align: "left",
        menuAlign: "left",
        dialogWidth: "40%",
        dialogClickModal: false,
        dialogType: "drawer",
        span: 24,
        dialogCustomClass: "huilan-drawer-form",
        emptyText: this.$t('aicosCommon.notData'),
        column: [
          {
            label: "动作名称",
            prop: "actionName",
            minWidth: 200,
            // search: true,
            searchSpan: 8,
            overHidden: true
          },
          {
            label: "动作id",
            prop: "actionIdUe",
            minWidth: 200,
            // search: true,
            searchSpan: 8,
            overHidden: true,
            rules: [
              {required: true, message: '请输入声音id', trigger: 'blur'},
              {min: 1, max: 50, message: '长度不可超过50个字符', trigger: 'blur'},
              {required: true, validator: checkType, trigger: 'blur'}
            ]
          },
          {
            label: '动作时长',
            prop: "actionSecond",
            width: '200'
          },
          {
            label: '预览动画',
            prop: 'actionVideoLink',
            width: '200',
            formatter: val => {
              if (val.actionType === 3) {

                return `<img style="object-fit: contain; width: 120px; height: 80px" src="${window.location.origin}/${val.actionVideoLink}"/>`
              } else {
                return `<video controls style="object-fit: contain; width: 160px; height: 100px" src="${window.location.origin}/${val.actionVideoLink}"></video>`
              }
            }
          },
          {
            label: '动作含义',
            prop: 'actionMeaning',
            minWidth: 200
          },
          {
            label: '类型',
            prop: "actionType",
            dicData: [
              {
                label: 'Web3D',
                value: 3
              },
              {
                label: 'UE3D',
                value: 4
              }
            ]
          },
          {
            label: "状态",
            prop: "status",
            type: "switch",
            dicData: [
              {
                label: "启用",
                value: 0,
              },
              {
                label: "禁用",
                value: 1,
              },
            ],
          },
          {
            label: "创建时间",
            minWidth: 150,
            prop: "createTime",
            overHidden: true,
            display: false
          },
          {
            label: "标签",
            prop: "soundTypeTag",
            hide: true
          },
          {
            label: "预览声音",
            prop: "soundVideoLink",
            hide: true,
            rules: [
              {required: true, message: '请上传声音文件', trigger: 'blur'},
            ]
          },
        ],
      },
      searchForm: {
        actionName: '',
        actionIdUe: '',
        actionType: '',
        actionMeaning: ''
      },
      searchFieldList: [
        {
          prop: 'actionName',
          placeholder: '请输入动作名称',
        },
        {
          prop: 'actionIdUe',
          placeholder: '请输入动作id',
        },
        {
          prop: 'actionMeaning',
          placeholder: '请输入动作含义',
        },
        {
          prop: 'actionType',
          type: 'select',
          dicData: [
            {
              label: 'Web3D',
              value: '3'
            },
            {
              label: 'UE3D',
              value: '4'
            }
          ]
        }
      ],
      selectedList: []
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    // handleClose(tag) {
    //   this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    // },
    //
    // showInput() {
    //   this.inputVisible = true;
    //   this.$nextTick(_ => {
    //     this.$refs.saveTagInput.$refs.input.focus();
    //   });
    // },
    //
    // handleInputConfirm() {
    //   let inputValue = this.inputValue;
    //   if (inputValue) {
    //     this.dynamicTags.push(inputValue);
    //   }
    //   this.inputVisible = false;
    //   this.inputValue = '';
    // },
    handleAdd() {
      // this.$refs.listRef.rowAdd()
      this.$refs.addAction.open('add', this.groupId);
    },
    currentChange(current) {
      this.page.currentPage = current
      this.getList()
    },
    sizeChange(size) {
      this.page.pageSize = size
      this.getList()
    },
    configColumn() {
      this.$refs.listRef.$refs.dialogColumn.columnBox = true;
    },
    searchFn() {
      this.page.currentPage = 1
      this.getList()
    },
    getList() {
      this.$nextTick(async () => {
        let params = {
          actionGroupId: this.groupId,
          ...this.searchForm,
          pageNo: this.page.currentPage,
          pageSize: this.page.pageSize
        }
        const {data} = await getActionPageList(params)
        this.tableData = data.data.records
        this.page.total = data.data.total
      })
    },
    selectionChange(selection) {
      this.selectedList = selection
    },
    async batchRemove() {
      if (this.selectedList.length === 0) {
        this.$message({
          type: "warning",
          message: "请选择需要处理的数据",
        });
        return false
      }

      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        let ids = this.selectedList.map(item => item.id)

        let params = {
          ids: ids.join(",")
        }
        const {data} = await deleteAction(params)
        this.$message({
          type: 'success',
          message: data.msg
        })
        this.selectedList = []
        this.getList()
      });

    },
    removeSound(row) {
      this.selectedList.push(row)
      this.batchRemove()
    },
    async toggleAction(row) {
      const {data} = await changeActionStatus({id: row.id, status: row.status ^ 1})
      console.log(data);
      this.getList()
    },
    previewRow(row) {
      this.$refs.addAction.open('preview', this.groupId, row)
    },
    editRow(row) {
      this.$refs.addAction.open('edit', this.groupId, row)
    },
    openBatchDialog() {
      this.$refs.addActionBatch.open(this.groupId)
    },
    playVideo(e) {
      e.target.loop = true
      e.target.play()
    },
    pauseVideo(e) {
      e.target.loop = false
      e.target.pause()
    }
  }
}
</script>

<template>
  <div style="padding: 0 30px 0">
    <!--    <HuilanBasicToolbar style="margin-top: 0">
          <el-button type="primary" size="small" @click="handleAdd">{{
              $t("aicosCommon.addBtn")
            }}
          </el-button>

          <el-button type="primary" size="small" @click="openBatchDialog">
            批量添加
          </el-button>
          <el-button type="danger" size="small" @click="batchRemove">
            批量删除
          </el-button>
        </HuilanBasicToolbar>-->

    <HuilanBasicSearchbar :model="searchForm" :fields="searchFieldList" @search="searchFn" @reset="getList">
      <template v-slot:appendTool>
        <el-button type="default" @click="configColumn"><i class="el-icon-s-operation"></i></el-button>
      </template>
    </HuilanBasicSearchbar>
    <avue-crud
      ref="listRef"
      v-model="form"
      :option="tableOptions"
      :page.sync="page"
      :data="tableData"
      @current-change="currentChange"
      @size-change="sizeChange"
      @row-save="saveSound"
      @selection-change="selectionChange"
    >
      <template #actionVideoLink="{row, $index}">
        <div v-if="row.actionType === 3">

          <img style="object-fit: contain; width: 120px; height: 80px"
               :src="`${window.location.origin}` + row.actionVideoLink"/>
        </div>
        <div v-else>
          <video :ref="`videoRef${$index}`"
                 muted
                 style="object-fit: contain; width: 160px; height: 100px"
                 :src="`${window.location.origin}/${row.actionVideoLink}`"
                 @mouseenter="playVideo($event)"
                 @mouseleave="pauseVideo($event)"
          ></video>
        </div>
      </template>
      <!--      <template slot-scope="scope" slot="menu">
              <el-button type="text" @click="previewRow(scope.row)">查看</el-button>
              <el-button type="text" @click="editRow(scope.row)">编辑</el-button>
              <el-button type="text" @click="toggleAction(scope.row)">
                {{ scope.row.status === 0 ? '停用' : '启用' }}
              </el-button>
              <el-button type="text" @click="removeSound(scope.row)">删除</el-button>
            </template>-->
    </avue-crud>


    <add-action
      ref="addAction"
      @save="getList"
    />

    <add-action-batch
      ref="addActionBatch"
      @save="getList"
    />
  </div>
</template>

<style scoped lang="scss">
.el-tag + .el-tag {
  margin-left: 10px;
}

.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}

.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
